/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package hxg.portmgr.core.dao.lineprofile.impl;

import hxg.portmgr.core.dao.DaoSource;
import hxg.portmgr.core.dao.LineProfileDAO;
import hxg.portmgr.core.device.Device;
import hxg.portmgr.core.device.DeviceType;
import hxg.portmgr.core.device.dslam.LineProfile;
import hxg.portmgr.core.oper.OperType;
import hxg.portmgr.core.oper.snmp.DslamSnmpOper;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.ConcurrentHashMap;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 *
 * @author hxg
 */
public abstract class AbsLineProfileDAO implements LineProfileDAO {

    protected ConcurrentHashMap<Long, List<LineProfile>> mp = new ConcurrentHashMap<Long, List<LineProfile>>();
    //File f = new File(".\\data\\LineProfile.xml");

    @Override
    public List<LineProfile> getProfiles(long devId) {
        if (mp.containsKey(devId)) {
            return mp.get(devId);
        } else {
            Device dev = DaoSource.getDeviceDao().getDeviceById(devId);
            if (dev.getType() == DeviceType.DSLAM) {
                try {
                    DslamSnmpOper oper = (DslamSnmpOper) dev.getOper(OperType.SnmpOper);
                    List<LineProfile> profs = oper.getLineProfiles();
                    linkLinePorfiles2Device(profs, devId);
                    return profs;
                } catch (IOException ex) {
                    Logger.getLogger(AbsLineProfileDAO.class.getName()).log(Level.SEVERE, null, ex);
                    return new ArrayList();
                }
            }
        }
        return new ArrayList();
    }

    @Override
    public List<LineProfile> getProfiles(Device dev) {
        return getProfiles(dev.getId());
    }

    @Override
    public void linkLinePorfiles2Device(List<LineProfile> profs, long devId) {
        mp.put(devId, profs);
    }
}
